<?php $section = 'Cart'; ?>
<?php include('inc/functions.php'); ?>
<?php
  include('Cart/cartFunctions.php');
  // The shopping cart needs sessions, so start one
  session_start();
  $new = $_GET['new'];
  $qtyEdit = $_POST['qtyEdit'];
  if ($qtyEdit < 0) {
  	$qtyEdit = 0;}
  $editTitle = $_POST['editTitle'];
  $clearCart = $_POST['clearCart'];
  if($clearCart)
  {
  	unset($_SESSION['cart']);
  	$_SESSION['items'] = 0;
  	$_SESSION['total_price'] = '0.00';
  }
  else if ($qtyEdit)
  {
  	if ($qtyEdit == 0) {
  		unset($_SESSION['cart'][$editTitle]);
  	}
  	else {
  		$_SESSION['cart'][$editTitle] = $qtyEdit;
  		$_SESSION['total_price'] = calculate_price($_SESSION['cart']);
   	 	$_SESSION['items'] = calculate_items($_SESSION['cart']);
    	header("Location: http://cs.newpaltz.edu/~N02045914/WebProject/cart.php");
    }
  }
  else if($new) {
    //new item selected
    if(!isset($_SESSION['cart'])) {
      $_SESSION['cart'] = array();
      $_SESSION['items'] = 0;
      $_SESSION['total_price'] ='0.00';
    }

    if(isset($_SESSION['cart'][$new])) {
      $_SESSION['cart'][$new]++;
    } else {
      $_SESSION['cart'][$new] = 1;
    }
    $_SESSION['total_price'] = calculate_price($_SESSION['cart']);
    $_SESSION['items'] = calculate_items($_SESSION['cart']);
    header("Location: http://cs.newpaltz.edu/~N02045914/WebProject/cart.php");
  }
  
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Core[nerd]-Cart</title>
<link href="inc/main.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="container">

<?php include('inc/nav.php'); ?>
<?php
	echo '<ul id="show_cart">
			<li>Cart Items: '.$_SESSION['items'].'</li>
			<li>Cart Total: $'.$_SESSION['total_price'].'</li>
		 </ul>';
?>

<h1>Shopping Cart</h1>
<?php
	if(($_SESSION['cart']) && (array_count_values($_SESSION['cart']) && (isLoggedIn()))) {
		display_cart($_SESSION['cart']); 
		echo '<form id="cart_footers" method="post"><input type="submit" name="clearCart" value="Clear Cart"></form>';
		echo '<form id="cart_footers" action="games.php"><input type="submit" value="Continue Shopping" /></br></form>';
		echo '<form id="cart_footers" method ="post" action="checkout.php?id='.$_SESSION['UserId'].'&cost='.$_SESSION['total_price'].'&games='.$_SESSION['cart'].'"><input type="submit" value="Proceed to Checkout" ></form>';
	}
	else 
    	echo "<p>There are no items in your cart</p>";
?>
</div>
</body>

</html>
